/**
 * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.triumphsys.icici.cmeeting.portlet.service.persistence;

import com.liferay.portal.NoSuchModelException;
import com.liferay.portal.kernel.annotation.BeanReference;
import com.liferay.portal.kernel.cache.CacheRegistryUtil;
import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.InstanceFactory;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.service.persistence.BatchSessionUtil;
import com.liferay.portal.service.persistence.ResourcePersistence;
import com.liferay.portal.service.persistence.UserPersistence;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

import com.triumphsys.icici.cmeeting.portlet.NoSuchCMPostProposalException;
import com.triumphsys.icici.cmeeting.portlet.model.CMPostProposal;
import com.triumphsys.icici.cmeeting.portlet.model.impl.CMPostProposalImpl;
import com.triumphsys.icici.cmeeting.portlet.model.impl.CMPostProposalModelImpl;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The persistence implementation for the c m post proposal service.
 *
 * <p>
 * Never modify or reference this class directly. Always use {@link CMPostProposalUtil} to access the c m post proposal persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
 * </p>
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author Triumphsys
 * @see CMPostProposalPersistence
 * @see CMPostProposalUtil
 * @generated
 */
public class CMPostProposalPersistenceImpl extends BasePersistenceImpl<CMPostProposal>
	implements CMPostProposalPersistence {
	public static final String FINDER_CLASS_NAME_ENTITY = CMPostProposalImpl.class.getName();
	public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
		".List";
	public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(CMPostProposalModelImpl.ENTITY_CACHE_ENABLED,
			CMPostProposalModelImpl.FINDER_CACHE_ENABLED,
			FINDER_CLASS_NAME_LIST, "findAll", new String[0]);
	public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(CMPostProposalModelImpl.ENTITY_CACHE_ENABLED,
			CMPostProposalModelImpl.FINDER_CACHE_ENABLED,
			FINDER_CLASS_NAME_LIST, "countAll", new String[0]);

	/**
	 * Caches the c m post proposal in the entity cache if it is enabled.
	 *
	 * @param cmPostProposal the c m post proposal to cache
	 */
	public void cacheResult(CMPostProposal cmPostProposal) {
		EntityCacheUtil.putResult(CMPostProposalModelImpl.ENTITY_CACHE_ENABLED,
			CMPostProposalImpl.class, cmPostProposal.getPrimaryKey(),
			cmPostProposal);
	}

	/**
	 * Caches the c m post proposals in the entity cache if it is enabled.
	 *
	 * @param cmPostProposals the c m post proposals to cache
	 */
	public void cacheResult(List<CMPostProposal> cmPostProposals) {
		for (CMPostProposal cmPostProposal : cmPostProposals) {
			if (EntityCacheUtil.getResult(
						CMPostProposalModelImpl.ENTITY_CACHE_ENABLED,
						CMPostProposalImpl.class,
						cmPostProposal.getPrimaryKey(), this) == null) {
				cacheResult(cmPostProposal);
			}
		}
	}

	/**
	 * Clears the cache for all c m post proposals.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	public void clearCache() {
		CacheRegistryUtil.clear(CMPostProposalImpl.class.getName());
		EntityCacheUtil.clearCache(CMPostProposalImpl.class.getName());
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
	}

	/**
	 * Clears the cache for the c m post proposal.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	public void clearCache(CMPostProposal cmPostProposal) {
		EntityCacheUtil.removeResult(CMPostProposalModelImpl.ENTITY_CACHE_ENABLED,
			CMPostProposalImpl.class, cmPostProposal.getPrimaryKey());
	}

	/**
	 * Creates a new c m post proposal with the primary key. Does not add the c m post proposal to the database.
	 *
	 * @param postProposalId the primary key for the new c m post proposal
	 * @return the new c m post proposal
	 */
	public CMPostProposal create(long postProposalId) {
		CMPostProposal cmPostProposal = new CMPostProposalImpl();

		cmPostProposal.setNew(true);
		cmPostProposal.setPrimaryKey(postProposalId);

		return cmPostProposal;
	}

	/**
	 * Removes the c m post proposal with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param primaryKey the primary key of the c m post proposal to remove
	 * @return the c m post proposal that was removed
	 * @throws com.liferay.portal.NoSuchModelException if a c m post proposal with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public CMPostProposal remove(Serializable primaryKey)
		throws NoSuchModelException, SystemException {
		return remove(((Long)primaryKey).longValue());
	}

	/**
	 * Removes the c m post proposal with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param postProposalId the primary key of the c m post proposal to remove
	 * @return the c m post proposal that was removed
	 * @throws com.triumphsys.icici.cmeeting.portlet.NoSuchCMPostProposalException if a c m post proposal with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public CMPostProposal remove(long postProposalId)
		throws NoSuchCMPostProposalException, SystemException {
		Session session = null;

		try {
			session = openSession();

			CMPostProposal cmPostProposal = (CMPostProposal)session.get(CMPostProposalImpl.class,
					new Long(postProposalId));

			if (cmPostProposal == null) {
				if (_log.isWarnEnabled()) {
					_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
						postProposalId);
				}

				throw new NoSuchCMPostProposalException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
					postProposalId);
			}

			return remove(cmPostProposal);
		}
		catch (NoSuchCMPostProposalException nsee) {
			throw nsee;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	protected CMPostProposal removeImpl(CMPostProposal cmPostProposal)
		throws SystemException {
		cmPostProposal = toUnwrappedModel(cmPostProposal);

		Session session = null;

		try {
			session = openSession();

			BatchSessionUtil.delete(session, cmPostProposal);
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);

		EntityCacheUtil.removeResult(CMPostProposalModelImpl.ENTITY_CACHE_ENABLED,
			CMPostProposalImpl.class, cmPostProposal.getPrimaryKey());

		return cmPostProposal;
	}

	public CMPostProposal updateImpl(
		com.triumphsys.icici.cmeeting.portlet.model.CMPostProposal cmPostProposal,
		boolean merge) throws SystemException {
		cmPostProposal = toUnwrappedModel(cmPostProposal);

		Session session = null;

		try {
			session = openSession();

			BatchSessionUtil.update(session, cmPostProposal, merge);

			cmPostProposal.setNew(false);
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);

		EntityCacheUtil.putResult(CMPostProposalModelImpl.ENTITY_CACHE_ENABLED,
			CMPostProposalImpl.class, cmPostProposal.getPrimaryKey(),
			cmPostProposal);

		return cmPostProposal;
	}

	protected CMPostProposal toUnwrappedModel(CMPostProposal cmPostProposal) {
		if (cmPostProposal instanceof CMPostProposalImpl) {
			return cmPostProposal;
		}

		CMPostProposalImpl cmPostProposalImpl = new CMPostProposalImpl();

		cmPostProposalImpl.setNew(cmPostProposal.isNew());
		cmPostProposalImpl.setPrimaryKey(cmPostProposal.getPrimaryKey());

		cmPostProposalImpl.setPostProposalId(cmPostProposal.getPostProposalId());
		cmPostProposalImpl.setCodCcmDt(cmPostProposal.getCodCcmDt());
		cmPostProposalImpl.setBranchId(cmPostProposal.getBranchId());
		cmPostProposalImpl.setApprovalTl(cmPostProposal.getApprovalTl());
		cmPostProposalImpl.setApprovalRmg(cmPostProposal.getApprovalRmg());
		cmPostProposalImpl.setCoePTypeId(cmPostProposal.getCoePTypeId());
		cmPostProposalImpl.setRatingRmgId(cmPostProposal.getRatingRmgId());
		cmPostProposalImpl.setSmId(cmPostProposal.getSmId());
		cmPostProposalImpl.setTlId(cmPostProposal.getTlId());
		cmPostProposalImpl.setIpNetM(cmPostProposal.getIpNetM());
		cmPostProposalImpl.setProposalName(cmPostProposal.getProposalName());
		cmPostProposalImpl.setReportingAId1(cmPostProposal.getReportingAId1());
		cmPostProposalImpl.setProgram(cmPostProposal.getProgram());
		cmPostProposalImpl.setProgramExpery(cmPostProposal.getProgramExpery());
		cmPostProposalImpl.setProposalId(cmPostProposal.getProposalId());

		return cmPostProposalImpl;
	}

	/**
	 * Finds the c m post proposal with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
	 *
	 * @param primaryKey the primary key of the c m post proposal to find
	 * @return the c m post proposal
	 * @throws com.liferay.portal.NoSuchModelException if a c m post proposal with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public CMPostProposal findByPrimaryKey(Serializable primaryKey)
		throws NoSuchModelException, SystemException {
		return findByPrimaryKey(((Long)primaryKey).longValue());
	}

	/**
	 * Finds the c m post proposal with the primary key or throws a {@link com.triumphsys.icici.cmeeting.portlet.NoSuchCMPostProposalException} if it could not be found.
	 *
	 * @param postProposalId the primary key of the c m post proposal to find
	 * @return the c m post proposal
	 * @throws com.triumphsys.icici.cmeeting.portlet.NoSuchCMPostProposalException if a c m post proposal with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public CMPostProposal findByPrimaryKey(long postProposalId)
		throws NoSuchCMPostProposalException, SystemException {
		CMPostProposal cmPostProposal = fetchByPrimaryKey(postProposalId);

		if (cmPostProposal == null) {
			if (_log.isWarnEnabled()) {
				_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + postProposalId);
			}

			throw new NoSuchCMPostProposalException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
				postProposalId);
		}

		return cmPostProposal;
	}

	/**
	 * Finds the c m post proposal with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param primaryKey the primary key of the c m post proposal to find
	 * @return the c m post proposal, or <code>null</code> if a c m post proposal with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public CMPostProposal fetchByPrimaryKey(Serializable primaryKey)
		throws SystemException {
		return fetchByPrimaryKey(((Long)primaryKey).longValue());
	}

	/**
	 * Finds the c m post proposal with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param postProposalId the primary key of the c m post proposal to find
	 * @return the c m post proposal, or <code>null</code> if a c m post proposal with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public CMPostProposal fetchByPrimaryKey(long postProposalId)
		throws SystemException {
		CMPostProposal cmPostProposal = (CMPostProposal)EntityCacheUtil.getResult(CMPostProposalModelImpl.ENTITY_CACHE_ENABLED,
				CMPostProposalImpl.class, postProposalId, this);

		if (cmPostProposal == null) {
			Session session = null;

			try {
				session = openSession();

				cmPostProposal = (CMPostProposal)session.get(CMPostProposalImpl.class,
						new Long(postProposalId));
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (cmPostProposal != null) {
					cacheResult(cmPostProposal);
				}

				closeSession(session);
			}
		}

		return cmPostProposal;
	}

	/**
	 * Finds all the c m post proposals.
	 *
	 * @return the c m post proposals
	 * @throws SystemException if a system exception occurred
	 */
	public List<CMPostProposal> findAll() throws SystemException {
		return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
	}

	/**
	 * Finds a range of all the c m post proposals.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of c m post proposals to return
	 * @param end the upper bound of the range of c m post proposals to return (not inclusive)
	 * @return the range of c m post proposals
	 * @throws SystemException if a system exception occurred
	 */
	public List<CMPostProposal> findAll(int start, int end)
		throws SystemException {
		return findAll(start, end, null);
	}

	/**
	 * Finds an ordered range of all the c m post proposals.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of c m post proposals to return
	 * @param end the upper bound of the range of c m post proposals to return (not inclusive)
	 * @param orderByComparator the comparator to order the results by
	 * @return the ordered range of c m post proposals
	 * @throws SystemException if a system exception occurred
	 */
	public List<CMPostProposal> findAll(int start, int end,
		OrderByComparator orderByComparator) throws SystemException {
		Object[] finderArgs = new Object[] {
				String.valueOf(start), String.valueOf(end),
				String.valueOf(orderByComparator)
			};

		List<CMPostProposal> list = (List<CMPostProposal>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
				finderArgs, this);

		if (list == null) {
			StringBundler query = null;
			String sql = null;

			if (orderByComparator != null) {
				query = new StringBundler(2 +
						(orderByComparator.getOrderByFields().length * 3));

				query.append(_SQL_SELECT_CMPOSTPROPOSAL);

				appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
					orderByComparator);

				sql = query.toString();
			}
			else {
				sql = _SQL_SELECT_CMPOSTPROPOSAL.concat(CMPostProposalModelImpl.ORDER_BY_JPQL);
			}

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				if (orderByComparator == null) {
					list = (List<CMPostProposal>)QueryUtil.list(q,
							getDialect(), start, end, false);

					Collections.sort(list);
				}
				else {
					list = (List<CMPostProposal>)QueryUtil.list(q,
							getDialect(), start, end);
				}
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					FinderCacheUtil.removeResult(FINDER_PATH_FIND_ALL,
						finderArgs);
				}
				else {
					cacheResult(list);

					FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs,
						list);
				}

				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Removes all the c m post proposals from the database.
	 *
	 * @throws SystemException if a system exception occurred
	 */
	public void removeAll() throws SystemException {
		for (CMPostProposal cmPostProposal : findAll()) {
			remove(cmPostProposal);
		}
	}

	/**
	 * Counts all the c m post proposals.
	 *
	 * @return the number of c m post proposals
	 * @throws SystemException if a system exception occurred
	 */
	public int countAll() throws SystemException {
		Object[] finderArgs = new Object[0];

		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
				finderArgs, this);

		if (count == null) {
			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(_SQL_COUNT_CMPOSTPROPOSAL);

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
					count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Initializes the c m post proposal persistence.
	 */
	public void afterPropertiesSet() {
		String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
					com.liferay.util.service.ServiceProps.get(
						"value.object.listener.com.triumphsys.icici.cmeeting.portlet.model.CMPostProposal")));

		if (listenerClassNames.length > 0) {
			try {
				List<ModelListener<CMPostProposal>> listenersList = new ArrayList<ModelListener<CMPostProposal>>();

				for (String listenerClassName : listenerClassNames) {
					listenersList.add((ModelListener<CMPostProposal>)InstanceFactory.newInstance(
							listenerClassName));
				}

				listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
			}
			catch (Exception e) {
				_log.error(e);
			}
		}
	}

	public void destroy() {
		EntityCacheUtil.removeCache(CMPostProposalImpl.class.getName());
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
	}

	@BeanReference(type = CMHolidayPersistence.class)
	protected CMHolidayPersistence cmHolidayPersistence;
	@BeanReference(type = CMParameterPersistence.class)
	protected CMParameterPersistence cmParameterPersistence;
	@BeanReference(type = CMUserPersistence.class)
	protected CMUserPersistence cmUserPersistence;
	@BeanReference(type = CMCreditRatingPersistence.class)
	protected CMCreditRatingPersistence cmCreditRatingPersistence;
	@BeanReference(type = COEPropoalTypePersistence.class)
	protected COEPropoalTypePersistence coePropoalTypePersistence;
	@BeanReference(type = CMIndustryPersistence.class)
	protected CMIndustryPersistence cmIndustryPersistence;
	@BeanReference(type = CMCompanyPersistence.class)
	protected CMCompanyPersistence cmCompanyPersistence;
	@BeanReference(type = CMControlGroupPersistence.class)
	protected CMControlGroupPersistence cmControlGroupPersistence;
	@BeanReference(type = CMRolePersistence.class)
	protected CMRolePersistence cmRolePersistence;
	@BeanReference(type = CMBranchPersistence.class)
	protected CMBranchPersistence cmBranchPersistence;
	@BeanReference(type = CMRegionPersistence.class)
	protected CMRegionPersistence cmRegionPersistence;
	@BeanReference(type = CMForumPersistence.class)
	protected CMForumPersistence cmForumPersistence;
	@BeanReference(type = CMSubForumPersistence.class)
	protected CMSubForumPersistence cmSubForumPersistence;
	@BeanReference(type = CMSanctionProposalPersistence.class)
	protected CMSanctionProposalPersistence cmSanctionProposalPersistence;
	@BeanReference(type = CMReportingProposalPersistence.class)
	protected CMReportingProposalPersistence cmReportingProposalPersistence;
	@BeanReference(type = CMPostProposalPersistence.class)
	protected CMPostProposalPersistence cmPostProposalPersistence;
	@BeanReference(type = CMMeetingPersistence.class)
	protected CMMeetingPersistence cmMeetingPersistence;
	@BeanReference(type = CMProposalTypePersistence.class)
	protected CMProposalTypePersistence cmProposalTypePersistence;
	@BeanReference(type = CMEntityPersistence.class)
	protected CMEntityPersistence cmEntityPersistence;
	@BeanReference(type = CMOthersProposalPersistence.class)
	protected CMOthersProposalPersistence cmOthersProposalPersistence;
	@BeanReference(type = CMValuationProposalPersistence.class)
	protected CMValuationProposalPersistence cmValuationProposalPersistence;
	@BeanReference(type = CMPoliciesProposalPersistence.class)
	protected CMPoliciesProposalPersistence cmPoliciesProposalPersistence;
	@BeanReference(type = CMProposalPersistence.class)
	protected CMProposalPersistence cmProposalPersistence;
	@BeanReference(type = ResourcePersistence.class)
	protected ResourcePersistence resourcePersistence;
	@BeanReference(type = UserPersistence.class)
	protected UserPersistence userPersistence;
	private static final String _SQL_SELECT_CMPOSTPROPOSAL = "SELECT cmPostProposal FROM CMPostProposal cmPostProposal";
	private static final String _SQL_COUNT_CMPOSTPROPOSAL = "SELECT COUNT(cmPostProposal) FROM CMPostProposal cmPostProposal";
	private static final String _ORDER_BY_ENTITY_ALIAS = "cmPostProposal.";
	private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No CMPostProposal exists with the primary key ";
	private static Log _log = LogFactoryUtil.getLog(CMPostProposalPersistenceImpl.class);
}